package com.timeste.lasttime;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ListActivity {
	
	private static Context mContext;
	private DatabaseHandler db;
	
	private TextView myText = null;
	
	public static Context getContext(){
        return mContext;
    }
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        mContext = this;
        setContentView(R.layout.main);
        db = new DatabaseHandler(this);
        
        readAllEvents();
        
    }
    
    public void createEvent(View view) {
    	Intent intent = new Intent(mContext, CreateEventActivity.class);
    	startActivity(intent);
    }
    
    public void readAllEvents() {
    	// Reading all contacts
        Log.d("Reading: ", "Reading all events..");
        List<LastTimeEvent> events = db.getAllEvents();
        
//        Collections.sort(events, new LastTimeEvent());
        
        List<String> eventsList = new ArrayList<String>();
        for (LastTimeEvent e : events) {
            // Writing Contacts to log
            Log.d("LastTimeEvent: ", e.toString());
            
            eventsList.add(e.get_title());
        }
        
        LastTimeEvent[] eventsAry = new LastTimeEvent[events.size()];
        events.toArray(eventsAry); 
        
        ListView myListView = (ListView)findViewById(android.R.id.list);

        ArrayAdapter<LastTimeEvent> aa = new EventAdapter (mContext,eventsAry);

//        aa.sort(new LastTimeEvent());
        myListView.setAdapter(aa);
        
	}
 
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Log.i("INFO","hey yo");
		//get selected items
//		String selectedValue = (String) getListAdapter().getItem(position);
//		Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();
 
	}
        
}

